package com.hsy.bibc.modules.code.zxing.camera;

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.view.SurfaceHolder;

import java.io.IOException;

/**
 * Created by MartinKent on 2016/6/1.
 */
public class CameraManager {
    static final int SDK_INT; // Later we can use Build.VERSION.SDK_INT

    static {
        SDK_INT = CameraManagerZXing.SDK_INT;
    }

    private Rect customRect = null;

    private static CameraManagerZXing cameraManagerZXing;
    private static CameraManager cameraManager = null;

    private CameraManager() {

    }

    public static void init(Context context) {
        if (cameraManager == null) {
            CameraManagerZXing.init(context);

            cameraManagerZXing = CameraManagerZXing.get();
            cameraManager = new CameraManager();
        }
    }

    public static CameraManager get() {
        return cameraManager;
    }

    public void openDriver(SurfaceHolder holder) throws IOException {
        cameraManagerZXing.openDriver(holder);
    }

    public void closeDriver() {
        cameraManagerZXing.closeDriver();
    }

    public void startPreview() {
        cameraManagerZXing.startPreview();
    }

    public void stopPreview() {
        cameraManagerZXing.stopPreview();
    }

    public void requestPreviewFrame(Handler handler, int message) {
        cameraManagerZXing.requestPreviewFrame(handler, message);
    }

    public void requestAutoFocus(Handler handler, int message) {
        cameraManagerZXing.requestAutoFocus(handler, message);
    }

    public void setCustomRect(Rect rect) {
        this.customRect = new Rect(rect);
    }

    public Rect getFramingRect() {
        Rect rect = null == customRect ? cameraManagerZXing.getFramingRect() : customRect;
        return rect;
    }

    public Rect getFramingRectInPreview() {
        return cameraManagerZXing.getFramingRectInPreview();
    }

    public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
        return cameraManagerZXing.buildLuminanceSource(data, width, height);
    }

    public Context getContext() {
        return cameraManagerZXing.getContext();
    }
}
